package hut.game.actor;

import hut.game.GameActivity;
import hut.game.table.BanCo;
import hut.game.table.NuocDi;
import hut.game.table.VanCo;
import hut.game.unit.Chot;
import hut.game.unit.Ma;
import hut.game.unit.Phao;
import hut.game.unit.QuanCo;
import hut.game.unit.Sy;
import hut.game.unit.Tinh;
import hut.game.unit.Tuong;
import hut.game.unit.Xe;

import java.util.ArrayList;

import android.util.Log;


public class Computer extends Player {
	public static int deep =  3;
	public static NuocDi newmove = new NuocDi();
	static ArrayList<NuocDi> arrMoveAI = new ArrayList<NuocDi>();
	static ArrayList<NuocDi> arrMoveHuman = new ArrayList<NuocDi>();
	public static final int INFINITY = 99999;
	@Override
	public void KhoiTao() {
		super.KhoiTao();
	}
	
	public Computer(int x) {
		super(x,GameActivity.mOnTouch);
	}
	//--------------------- Ham reset Computer
	public void ResetComputer(){
		newmove = new NuocDi();
		dsQuanCo = new ArrayList<QuanCo>();
    	qTuong = new Tuong();
        qSy[0] = new Sy();
        qSy[1] = new Sy();
        qTinh[0] = new Tinh();
        qTinh[1] = new Tinh();
        qXe[0] = new Xe();
        qXe[1] = new Xe();
        qPhao[0] = new Phao();
        qPhao[1] = new Phao();
        qMa[0] = new Ma();
        qMa[1] = new Ma();
        qChot[0] = new Chot();
        qChot[1] = new Chot();
        qChot[2] = new Chot();
        qChot[3] = new Chot();
        qChot[4] = new Chot();
	}
	
	//--------------------- Ham thuc hien nuoc di cua Computer
	public static void ComputerThink()
    {
        if (VanCo.LuotDi == VanCo.computer.Phe )
        {
        	if(VanCo.computer != null){
        		Log.d("d", "" + Computer.deep);
        		Computer.AlphaBeta(Computer.deep,-INFINITY,INFINITY,VanCo.LuotDi);
        		if(Computer.newmove !=  null){
        			Log.d("Move", newmove.Dau.Ten + "/" + newmove.Dau.TrangThai + "/" + newmove.score);
            		if(Computer.newmove.Dau.Ten != "")
            		{
            			  //Thiet lap nuoc di o day
                        for (int i = 0; i < VanCo.computer.dsQuanCo.size(); i++)
                	    {
                	      	if (Computer.newmove.Dau.Ten == VanCo.computer.dsQuanCo.get(i).Ten && Computer.newmove.Dau.ThuTu == VanCo.computer.dsQuanCo.get(i).ThuTu)
                	        {
                	             VanCo.DanhDau = VanCo.computer.dsQuanCo.get(i);
                	             break;
                	        }
                	    }
                        int hc = Computer.newmove.Hang_Cuoi;
                        int cc = Computer.newmove.Cot_Cuoi;
                        //Ô cờ trống tại ví trí ban đầu                
                        VanCo.OCoTrong(VanCo.DanhDau.Hang, VanCo.DanhDau.Cot);
                        //Ghi thông tin nước đi vào GameLog
                        if (Computer.newmove.Cuoi.Ten != "")
                        {
                            for (int i = 0; i < VanCo.n1.dsQuanCo.size(); i++)
                            {
                                if (Computer.newmove.Cuoi.Ten == VanCo.n1.dsQuanCo.get(i).Ten && Computer.newmove.Cuoi.ThuTu == VanCo.n1.dsQuanCo.get(i).ThuTu)
                                {
                                    QuanCo temp = VanCo.n1.dsQuanCo.get(i);
                                    VanCo.LuuVaoGameLog(BanCo.ViTri[temp.Hang][temp.Cot].imgMove, temp);
                                    VanCo.AnQuanCo(temp);
                                    break;
                                }
                            }
                        }
                        else
                            VanCo.LuuVaoGameLog(BanCo.ViTri[Computer.newmove.Dau.Hang][Computer.newmove.Dau.Cot].imgMove, VanCo.DanhDau);
                        VanCo.ply++;
                        //Đặt quân cờ đã chọn vào vị trí mới [VanCo.AI.newmove.Hang_Cuoi, VanCo.AI.newmove.Cot_Cuoi]
                        BanCo.ViTri[hc][cc].Trong = false;
                        BanCo.ViTri[hc][cc].Phe = VanCo.DanhDau.Phe;
                        BanCo.ViTri[hc][cc].Ten = VanCo.DanhDau.Ten;
                        BanCo.ViTri[hc][cc].ThuTu = VanCo.DanhDau.ThuTu;
                        VanCo.DanhDau.Hang = hc;
                        VanCo.DanhDau.Cot = cc;
                        if(VanCo.DanhDau.isFirst){
                        	VanCo.DanhDau.TrangThai = 1;
                        	VanCo.DanhDau.isFirst = false;
            			}
                        VanCo.DanhDau.drawImage();
                        GameActivity.setLayout(VanCo.DanhDau.imgUnit, VanCo.DanhDau.Cot * 51 + 10, VanCo.DanhDau.Hang * 70  + 98, 0, 0);
                        //Thay đổi lượt đi
                        Log.d("luotdi", "doi luot di trong computer think");
                        VanCo.DoiLuotDi();
                        //Kiểm tra chiếu bí
                        VanCo.KiemTraChieuBi();
            		}
        		}else{
        			VanCo.winner = 1;
        		}
        		
        	}else{
        		VanCo.winner = 1;
        	}
        	if (VanCo.winner != 2)
			{
				if(VanCo.state)
				{
					if (VanCo.winner == 0) GameActivity.ThongBaoKetThuc.KhoiTao("Player 1 has winner", 1);
					if (VanCo.winner == 1) GameActivity.ThongBaoKetThuc.KhoiTao("Player 2 has winner", 1);
				}else
				{
					if (VanCo.winner == 0) GameActivity.ThongBaoKetThuc.KhoiTao("Computer has winner", 1);
					if (VanCo.winner == 1) GameActivity.ThongBaoKetThuc.KhoiTao("Player 1 has winner", 1);
				}
				GameActivity.showResult();
				BanCo.KhoaBanCo();
			}
            //Kiểm tra chiếu tướng
            VanCo.KiemTraChieuTuong();
            BanCo.ResetCanMove();
        }
    }
	
	public static int AlphaBeta(int depth,int alpha,int beta,int luotdi){
		newmove = new NuocDi();
		if(depth == 0){
			return Eval.TinhTongDiemBanCo();
		}
		if(luotdi == 0){
			if(VanCo.ply == 0){
				arrMoveAI.clear();
				arrMoveAI = Gen.SinhNuocDiDauChoAI();
				for(int i = 0; i < arrMoveAI.size() - 1; i++){
					for(int j = 0; j < arrMoveAI.size() - i - 1; j++)
					{
						if(arrMoveAI.get(j).Dau.getDiem() > arrMoveAI.get(j+1).Dau.getDiem()){
							newmove = arrMoveAI.get(j);
							alpha = Math.max(alpha, newmove.score);
							if(beta <= alpha){
								break;
							}
						}else{
							newmove = arrMoveAI.get(j+1);
							alpha = Math.max(alpha, newmove.score);
							if(beta <= alpha){
								break;
							}
						}
					}
				}
				return alpha;
			}
			else{
				arrMoveAI.clear();
				arrMoveAI = Gen.SinhNuocDiChoAI();
				for(int i = 0; i < arrMoveAI.size() - 1; i++){
					try {
						Gen.MakeMove(arrMoveAI.get(i));
						alpha = Math.max(alpha, AlphaBeta(depth - 1, alpha, beta, luotdi==1?0:1));
						Gen.UnMakeMove(arrMoveAI.get(i));
						if(depth == Computer.deep)
							newmove = arrMoveAI.get(i);
						if(beta <= alpha){
							break;
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				return alpha;
			}
		}else{
			arrMoveHuman.clear();
			arrMoveHuman = Gen.SinhNuocChoNguoiChoi();
			for(int i = 0; i < arrMoveHuman.size() - 1; i++){
				try {
					Gen.MakeMove(arrMoveHuman.get(i));
					beta = Math.min(beta, AlphaBeta(depth - 1, alpha, beta, luotdi==1?0:1));
					Gen.UnMakeMove(arrMoveHuman.get(i));
					if(beta <= alpha)
						break;
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			return beta;
		}
	}
}
